package com.dm.wallpaper.library.fraction;

import com.dm.wallpaper.library.ResourceTable;
import com.dm.wallpaper.library.ablility.WallpaperAbility;
import com.dm.wallpaper.library.ablility.WallpaperBoardBrowserAbility;
import com.dm.wallpaper.library.adapters.MainProvider;
import com.dm.wallpaper.library.helpers.AnimatorHelp;
import com.dm.wallpaper.library.items.PopupItem;
import com.dm.wallpaper.library.preferences.Preferences;
import com.dm.wallpaper.library.utils.Log;
import com.dm.wallpaper.library.utils.DialogUtils;
import com.dm.wallpaper.library.utils.SystemUtils;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.animation.Animator;
import ohos.agp.components.*;
import ohos.agp.window.dialog.PopupDialog;

public class CollectionWallpaperFraction extends Fraction implements Component.ClickedListener {
    private Component component;
    private MainProvider mainProvider;
    private PageSlider pageSlider;
    private Component line;
    private Image sort_wallpaper;
    private int nowPage;
    private PopupDialog popupDialog;

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        component = scatter.parse(ResourceTable.Layout_fraction_collection_wallpaper, container, false);
        if (mainProvider == null) {
            mainProvider = new MainProvider(getFractionAbility());
        }
        initSlider();
        return component;
    }

    private void initSlider() {
        Log.i("CollectionWallpaperFraction.initSlider([]):43   " + nowPage);
        if (pageSlider != null) {
            pageSlider.removePageChangedListener(pageChangedListener);
        }
        pageSlider = (PageSlider) component.findComponentById(ResourceTable.Id_page_slider);
        pageSlider.setProvider(mainProvider);
        pageSlider.addPageChangedListener(pageChangedListener);

    }

    private PageSlider.PageChangedListener pageChangedListener = new PageSlider.PageChangedListener() {
        int state = 0;

        @Override
        public void onPageSliding(int i, float v, int i1) {
            int nowPage = pageSlider.getCurrentPage();
            double v1 = v;
            double tx = 0;
            if (state == 1) {
                if (i1 > 0) {
                    tx = SystemUtils.getDisplayWidthInPx(getFractionAbility()) / (double) 3 * i + SystemUtils.getDisplayWidthInPx(getFractionAbility()) / (double) 3 * v1;
                } else {
                    tx = SystemUtils.getDisplayWidthInPx(getFractionAbility()) / (double) 3 * i - SystemUtils.getDisplayWidthInPx(getFractionAbility()) / (double) 3 * v1;
                }
            } else if (state == 2) {
                if (i1 > 0) {
                    tx = SystemUtils.getDisplayWidthInPx(getFractionAbility()) / (double) 3 * i + SystemUtils.getDisplayWidthInPx(getFractionAbility()) / (double) 3 * (nowPage - i) * v1;
                } else {
                    tx = SystemUtils.getDisplayWidthInPx(getFractionAbility()) / (double) 3 * i - SystemUtils.getDisplayWidthInPx(getFractionAbility()) / (double) 3 * (i - nowPage) * v1;
                }
            }
            line.setTranslationX((float) tx);
        }

        @Override
        public void onPageSlideStateChanged(int i) {
            state = i;

        }

        @Override
        public void onPageChosen(int i) {
            nowPage = i;
            line.setTranslationX(SystemUtils.getDisplayWidthInPx(getFractionAbility()) / 3 * nowPage);
            if (i == 1) {
                AnimatorHelp.scaleXYAnimator(sort_wallpaper, new AnimatorHelp.SimpleStateChangedListener() {
                    @Override
                    public void onStart(Animator animator) {
                        super.onStart(animator);
                        sort_wallpaper.setVisibility(Component.VISIBLE);
                    }

                    @Override
                    public void onEnd(Animator animator) {
                        super.onEnd(animator);
                        sort_wallpaper.setVisibility(Component.VISIBLE);
                    }
                });
            } else {
                AnimatorHelp.scaleXYAnimatorToHid(sort_wallpaper, new AnimatorHelp.SimpleStateChangedListener() {
                    @Override
                    public void onEnd(Animator animator) {
                        super.onEnd(animator);
                        sort_wallpaper.setVisibility(Component.HIDE);
                    }
                });
            }
        }
    };

    @Override
    protected void onActive() {
        Log.i("CollectionWallpaperFraction.onActive([]):100");
        super.onActive();
        if (component == null) {
            return;
        }
        component.findComponentById(ResourceTable.Id_left_mune_icon).setClickedListener(this);
        component.findComponentById(ResourceTable.Id_latest_wallpaper).setClickedListener(this);
        component.findComponentById(ResourceTable.Id_collection_wallpapers).setClickedListener(this);
        component.findComponentById(ResourceTable.Id_collection_categories).setClickedListener(this);
        component.findComponentById(ResourceTable.Id_search_wallpaper).setClickedListener(this);
        line = component.findComponentById(ResourceTable.Id_bootom_line);
        sort_wallpaper = (Image) component.findComponentById(ResourceTable.Id_sort_wallpaper);
        ComponentContainer.LayoutConfig lineConfig = line.getLayoutConfig();
        lineConfig.width = SystemUtils.getDisplayWidthInPx(getFractionAbility()) / 3;
        line.setLayoutConfig(lineConfig);

        sort_wallpaper.setClickedListener(this);
        pageSlider.setCurrentPage(nowPage);

    }

    @Override
    public void onClick(Component component) {
        Log.i("CollectionWallpaperFraction.onClick([component]):138 ");
        if (component.getId() == ResourceTable.Id_left_mune_icon) {
            if (getFractionAbility() instanceof WallpaperAbility) {
                ((WallpaperAbility) getFractionAbility()).showMunePop();
            }
        } else if (component.getId() == ResourceTable.Id_latest_wallpaper) {
            pageSlider.setCurrentPage(0);
        } else if (component.getId() == ResourceTable.Id_collection_wallpapers) {
            pageSlider.setCurrentPage(1);
        } else if (component.getId() == ResourceTable.Id_collection_categories) {
            pageSlider.setCurrentPage(2);
        } else if (component.getId() == ResourceTable.Id_search_wallpaper) {
            Intent getIntent = new Intent();
            Operation opt = new Intent
                    .OperationBuilder()
                    .withBundleName(getFractionAbility().getBundleName())
                    .withAbilityName(WallpaperBoardBrowserAbility.class)
                    .build();
            getIntent.setOperation(opt);
            getFractionAbility().startAbility(getIntent);
        } else if (component.getId() == ResourceTable.Id_sort_wallpaper) {
            popupDialog= DialogUtils.getInstance().showSortDialog(getFractionAbility(), sort_wallpaper, new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    if (component.getId() == ResourceTable.Id_sort_latest) {
                        Preferences.get().setSortBy(PopupItem.Type.SORT_LATEST);
                    } else if (component.getId() == ResourceTable.Id_sort_oldest) {
                        Preferences.get().setSortBy(PopupItem.Type.SORT_OLDEST);
                    } else if (component.getId() == ResourceTable.Id_sort_name) {
                        Preferences.get().setSortBy(PopupItem.Type.SORT_NAME);
                    } else if (component.getId() == ResourceTable.Id_sort_random) {
                        Preferences.get().setSortBy(PopupItem.Type.SORT_RANDOM);
                    }
                    ((MainProvider) pageSlider.getProvider()).notifyWallpaper();
                    if (popupDialog != null)
                        popupDialog.destroy();
                }
            });
        }
    }
}
